//
//  SLHomeSlidMenuView.m
//  SlockApp
//
//  Created by 凯朱 on 2024/7/10.
//

#import "SLBridgeInfoVC.h"
#import "SLDeviceSettingCell.h"
#import "SLUpdateFirmVC.h"

static  NSString *const kCellId = @"SLDeviceSettingCell";
@interface SLBridgeInfoVC ()<UITableViewDelegate,UITableViewDataSource>{
    SLLockModel *_bridgeModel;
}

@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)UITableView *tableView;
@end

@implementation SLBridgeInfoVC

-(void)viewDidLoad{
    [super viewDidLoad];
   
    [self configDatas];
    [self setUpSubViews];
    [self loadData];
}

-(void)loadData{
    [ZCTool showLoadingOnKeyWindow];
    NSString *url = [NSString stringWithFormat:@"/msl_user/getDevInfo/%@",kCurClickDevice.bridgeDeviceNo];
    [ZKHttpManager getWithPath:url params:@{} finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull code, NSString * _Nonnull message) {
        [ZCTool hideLoadingOnKeyWindow];
        if (!isSuccessed) {
            kShowToast(message);
            return;
        }
        //更新页面
        NSDictionary *data = result[@"result"];
        self-> _bridgeModel = [SLLockModel new];
        self-> _bridgeModel.deviceName = data[@"deviceName"];
        self-> _bridgeModel.firmware = data[@"version"];
        self-> _bridgeModel.rssi = kSTRING(data[@"rssi"]);
        self-> _bridgeModel.wifiName = kSTRING(data[@"wifiName"]);
        self-> _bridgeModel.macAddress = kSTRING(data[@"macAddr"]);
        [self refreshSubViews];
    }];
}

-(void)refreshSubViews{
    [self configDatas];
    [_tableView reloadData];
}

-(void)setUpSubViews{
    self.title = kLocalString(@"Bridge management");
    [self.view addSubview:self.tableView];
    
//    ZKBaseButton *sureBtn = [ZKBaseButton buttonWithType:UIButtonTypeCustom];
//    sureBtn.frame = CGRectMake(20, kScreenHeight-SafeAreaBottomHeight-70, kScreenWidth-40, 50);
//    sureBtn.btnStyle = 2;
//    [sureBtn setTitle:kLocalString(@"Disconnect") forState:UIControlStateNormal];
//    [self.view addSubview:sureBtn];
//    [sureBtn addTarget:self action:@selector(disconnectAction) forControlEvents:UIControlEventTouchUpInside];
}

-(void)disconnectAction{
    
}

-(void)configDatas{
    [self.dataArray removeAllObjects];
    NSMutableArray *section = [NSMutableArray new];
    NSArray *sectionTitles = @[kLocalString(@"Name"),kLocalString(@"Wi-fi"),kLocalString(@"Model"),kLocalString(@"Mac"),kLocalString(@"Version")];
    NSString *wifiName = kReplace(_bridgeModel.wifiName, kLocalString(@"Disconnected"));
    NSArray *sectionContents = @[kReplace(_bridgeModel.deviceName, @"-"),wifiName,kReplace(_bridgeModel.model, @"-"),kSTRING(_bridgeModel.macAddress),kReplace(_bridgeModel.firmware, @"-")];
    
    
    [sectionTitles enumerateObjectsUsingBlock:^(NSString *title, NSUInteger idx, BOOL * _Nonnull stop) {
        SLDeviceSettingModel *model = [SLDeviceSettingModel new];
        model.title = title;
        model.style = 3;
        if ([title isEqualToString:kLocalString(@"Firmware")]) {
            model.style = 1;
            model.contentColor = KColor_O;
        }
        model.content = sectionContents[idx];
        [section addObject:model];
    }];
    [_dataArray addObject:section];
}

#pragma mark - ================<tableViewDataSource>===============
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *sections = _dataArray[section];
    return sections.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SLDeviceSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    SLDeviceSettingModel *model = _dataArray[indexPath.section][indexPath.row];
    cell.model = model;
    
    NSArray *sections = _dataArray[indexPath.section];
    cell.line.hidden = indexPath.row == sections.count-1;
    CGFloat height = [SLDeviceSettingCell cellHeightWith:model];
    [cell.contentView setRoundedCorners:0 forCorners:UIRectCornerAllCorners frame:CGRectMake(0, 0, kScreenWidth-40, height)];
    if (sections.count == 1) {
        [cell.contentView setRoundedCorners:20 forCorners:UIRectCornerAllCorners frame:CGRectMake(0, 0, kScreenWidth-40, height)];
    }else{
        if (indexPath.row == sections.count-1) {
            [cell.contentView setRoundedCorners:20.0 forCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight frame:CGRectMake(0, 0, kScreenWidth-40, height)];
        }else if (indexPath.row == 0){
            [cell.contentView setRoundedCorners:20.0 forCorners:UIRectCornerTopLeft | UIRectCornerTopRight frame:CGRectMake(0, 0, kScreenWidth-40, height)];
        }
    }
    return cell;
}

#pragma mark - ================<UITableViewDelegate>===============
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    SLDeviceSettingModel *model = _dataArray[indexPath.section][indexPath.row];
    return [SLDeviceSettingCell cellHeightWith:model];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UITableViewHeaderFooterView *headview = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"head"];
    if (!headview) {
        headview = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"head"];
        headview.frame = CGRectMake(0, 0, kScreenWidth, 30);
        headview.contentView.backgroundColor = KClearColor;
    }
    return headview;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"foot"];
    if (!footerView) {
        footerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"foot"];
        footerView.frame = CGRectMake(0, 0, kScreenWidth, 0.1);
        footerView.contentView.backgroundColor = KClearColor;
    }
    return footerView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SLDeviceSettingModel *model = _dataArray[indexPath.section][indexPath.row];
    if ([model.title isEqualToString:kLocalString(@"Firmware")]){
       SLUpdateFirmVC *vc = [[SLUpdateFirmVC alloc] init];
       [self.navigationController pushViewController:vc animated:YES];
   }
   
}

#pragma mark - ================<懒加载>===============
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, SafeAreaTopHeight, kScreenWidth-40, kScreenHeight-SafeAreaTopHeight-SafeAreaBottomHeight-70-15) style:UITableViewStyleGrouped];
        _tableView.estimatedRowHeight = 50;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:kCellId bundle:nil] forCellReuseIdentifier:kCellId];
        _tableView.backgroundColor = KClearColor;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

@end
